﻿using SixLabors.Fonts;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NetCoreClass
{
    public class CaptchaImage
    {
        private int _width;
        private int _height;
        private string _verifyCode;

        public CaptchaImage( int width, int height)
        {
            _height = height;
            _width = width;
        }

        public byte[] CreateByteByImgVerifyCode()
        {

            using Image image = new Image<Rgba32>(_width, _height);
            //漆底色白色
            image.Mutate(x => x.BackgroundColor(Color.White));
            //image.Mutate(x => x.DrawLines(Pens.DashDot(Color.White, _width), new PointF[] { new PointF() { X = 0, Y = 0 }, new PointF() { X = _width, Y = _height } }));

            FontCollection collection = new();
            FontFamily family = collection.Add("font/Inkfree.ttf");
            Font font = family.CreateFont(50, FontStyle.Bold);

            PointF startPointF = new PointF(5, 5);
            Random random = new Random(); //随机数产生器

            Color[] colors = new Color[] { Color.Red, Color.Blue, Color.Green, Color.Purple, Color.Peru, Color.LightSeaGreen, Color.Lime, Color.Magenta, Color.Maroon, Color.MediumBlue, Color.MidnightBlue, Color.Navy };
            //绘制大小
            for (int i = 0; i < _verifyCode.Length; i++)
            {
                image.Mutate(x => x.DrawText(_verifyCode[i].ToString(), font, colors[random.Next(colors.Length)], startPointF));
                //Console.WriteLine($"draw code:{verifyCode[i]} point:{startPointF.X}-{startPointF.Y}");
                startPointF.X += (int)(_width - 10) / _verifyCode.Length;
                startPointF.Y = random.Next(5, 10);
            }

            IPen pen = Pens.DashDot(Color.Silver, 1);

            ////绘制干扰线
            for (var k = 0; k < 40; k++)
            {
                PointF[] points = new PointF[2];
                points[0] = new PointF(random.Next(_width), random.Next(_height));
                points[1] = new PointF(random.Next(_width), random.Next(_height));
                image.Mutate(x => x.DrawLines(pen, points));
            }
           
            //image.Mutate(x => x.DrawText(_verifyCode,font, Color.Black, new PointF(20, 20)));


            using MemoryStream stream = new MemoryStream();
            image.Save(stream, new PngEncoder());
             //输出图片流  
            return stream.ToArray();

        }
        public string CreateValidateCode(int length)
        {
            string chkCode = string.Empty;
            //颜色列表，用于验证码、噪线、噪点 

            Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
            //字体列表，用于验证码 
            string[] font = { "Times New Roman", "Verdana", "Arial", "Gungsuh", "Impact" };
            //验证码的字符集，去掉了一些容易混淆的字符 
            char[] character = { 'a', 'c', 'd', 'e', 'f', 'g', 'k', 'm', 'p', 'r', 's', 't', 'w', 'x', 'y', 'A', 'C', 'D', 'E', 'F', 'G', 'K', 'M', 'P', 'R', 'S', 'T', 'W', 'X', 'Y', '3', '4', '5', '7', '8' };

            Random rnd = new Random();
            //生成验证码字符串 
            for (int i = 0; i < length; i++)
            {
                chkCode += character[rnd.Next(character.Length)];
            }
            _verifyCode=chkCode;
            return chkCode;
        }
    }
}
